package cn.xutongxue.book_sales.controller;



import cn.xutongxue.book_sales.base.Errors;
import cn.xutongxue.book_sales.base.Result;
import cn.xutongxue.book_sales.entity.Menu;

import cn.xutongxue.book_sales.service.MenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static cn.xutongxue.book_sales.base.Errors.UNKNOWN_ERROR;


/**
 * @author XuTongXue
 * @since 2021-12-02
 */

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @GetMapping("/getMenu")
    public Result getMenu() {

        QueryWrapper<Menu> wrapper = new QueryWrapper();
        wrapper
                .select("menu_id", "menu_name", "menu_path", "menu_component")
                .eq("menu_status", "1");
        List<Menu> list = menuService.list(wrapper);
        if(list != null || list == null)
            return Result.ok("获取菜单成功", list);
        return Result.fail(UNKNOWN_ERROR);
    }

    @PostMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu) {

        menu.setMenuName(menu.getMenuName());
        menu.setMenuComponent(menu.getMenuComponent());
        menu.setMenuPath(menu.getMenuPath());
        menu.setMenuStatus(menu.getMenuStatus());
        menuService.save(menu);

        if(menu.getMenuId() != null) {

            return Result.ok("添加菜单成功", null);
        }

        return Result.fail(601,"菜单添加失败");

    }

    @PostMapping("/delMenu")
    public Result deleteMenu(@RequestBody Menu menu) {

        if(menuService.removeById(menu.getMenuId()))
            return Result.ok("删除成功", null);
        return Result.fail(602, "菜单删除失败");
    }

    @PostMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu) {

        if(menuService.updateById(menu))
            return Result.ok("修改成功", null);
        return Result.fail(603, "菜单修改失败");
    }

}
